
var app=getApp();
var utils = require("../../utils/utils");
var P = require('../../utils/wxpage');
let  cartOperation = require('../../wxpage/cartOperation/cartOperation.js');
let  specifications = require('../../wxpage/specifications/specifications.js');
P('menu',{
    comps:[cartOperation,specifications],
    data: {
        noData:false,
        isHttp:true,
        menuIsShow:false,
        ejectIshow:false,
        bigIndex: 0,
        bigIdNum:"",
        bigclass: [], // 导航数组，通过ajax获取
        details: [], // 食品的对象数组
        cartData: [], // 购物车储存的对象数组
        movelength: 0,  // 上移或下拉动画的单位距离
        cartIsHidden: true, // 购物车是否隐藏
        cartIndexIsHidden: true, // 购物车详情菜单是否隐藏
        minusShow:true,
        p:1,//商品页数
        specifShow:false,
        selectedData:[]//规格数据
         //specifShow:true
    },
    // 设置标题名
    onReady: function () {
        wx.setNavigationBarTitle({
            title: '菜单'
        })
    },
    // 加载ajax的最佳时机
    onLoad: function (options) {
        var  that = this;
        var GGsysInfo = wx.getSystemInfoSync().windowHeight;
        wx.showLoading({
            title: '加载中'
        });
       that.getBigclass();
        that.setData({
            syncHeight: GGsysInfo
        });
        //console.log('menu------onLoad');
        //console.log(this.data.cartDataList);
    },
    onShow:function(){
        //////console.log('判断cart是否显示');

    },
    //点击一级分类事件
    changepage: function (e) {
        this.setData({
            p:1,
            noData:false
        });
        this.setData({
            bigIdNum:e.currentTarget.dataset.name,
            details:[],
            bigIndex:e.currentTarget.id
        });
        this.getClassA();
    },
    //获取一级分类  my
    getBigclass: function () {
        var that = this;
        var httpObj = {
            url: "FreshTwo/categorylist",
            data: {
                Who: app.globalData.who,
                 showfullpicsrc:1
            },
            thatthod: 'GET',
            success: function (res) {
                if(res.data.status==1){
                    that.setData({
                        bigclass: res.data.bigclass,
                        bigId:res.data.bigclass[0].bigclassid
                    });
                    ////////console.log("图片返回值");
                    ////////console.log(that.data.bigclass[0].picurl);
                    ////////console.log("第一个一级id");
                    ////////console.log(that.data.bigclass[0].bigclassid);
                    //获取默认商品列表
                    that.setData({
                        bigIdNum:that.data.bigclass[0].bigclassid
                    });
                    that.getClassA();
                }
            },
            fail: function (error) {
                //(error);
            }
        };
        utils.http(httpObj);
    },
    //请求一级分类下的全部商品
    getClassA:function(){
        var that=this;
        ////////console.log("请求此一级分类下的全部商品");
        utils.http({
            url:"FreshTwo/productcommon",
            data:{
                bigid:that.data.bigIdNum,
                Who:app.globalData.who,
                showfullpicsrc:1,
                P:that.data.p
            },
            success:function(res){
                //////console.log("商品请求--------------------------------请求成功");
                ////////console.log(res);
                if(res.statusCode==200){
                    if(res.data.productdata!="null"){
                        ////////console.log("判断成功");
                        that.setData({
                            details:that.data.details.concat(res.data.productdata),
                            p:that.data.p+1,
                            isHttp:true
                        });
                        ////////console.log("保存的数据");
                        ////////console.log(that.data.p);
                        ////////console.log(that.data.details);
                        //请求购物车数据
                        that.isCartData();
                        if(res.data.productdata.length<10){
                            that.setData({
                                noData:true
                            });
                        }
                    }else{
                        that.setData({
                            noData:true
                        });
                    }
                    setTimeout(function(){
                        wx.hideLoading();
                        that.setData({
                            menuIsShow:true
                        })

                    },0)
                }
            },
            fail: function (err) {
            }
        })
    },
    //加入购物车
    menuAddCart: function (e){
        console.log('menu 加入购物车');
        utils.addCart(e,this);
    },
    //弹出列表
    topEject:function(){
       this.setData({
            ejectIshow:!this.data.ejectIshow
        });
        ////////console.log("弹出列表事件");
    },
    //操作购物车数量
    operatings:function(e){
        console.log('menu页的 操作购物车');
        if(e.currentTarget.id=='iconReduce'){
            //console.log('图标减');
        }else{
            //console.log('非图标减');
            //console.log(e);
            if(e.currentTarget.dataset.specif){
                console.log('该条商品是带有属性的商品');
                let specif=e.currentTarget.dataset.specif;
                //console.log(specif);
                utils.operating(e,this,specif);
                //function addCart(e,t_this,specif,vm) {
               // utils.addCart(e,this,specif,null);
            }else{
                utils.operating(e,this);
            }
            //e,thats,specif

            //console.log('非图标操作');
            //console.log(e,this);
        }
    },
    //监听下拉
    onPullDownRefresh:function(){
        ////////console.log("下拉刷新");
        wx.stopPullDownRefresh();
    },
    //右侧商品的上拉加载
    pLoad:function(){
        var that=this;
        ////////console.log("右侧商品的上拉加载");
        ////////console.log("页数"+that.data.p);
        if(that.data.isHttp==true){
            that.setData({
                isHttp:false
            });
            that.getClassA();
        }else{
            ////////console.log("还没加载完");
        }
    },
    //显示操作列表
    isCartData:function(){
      //////console.log('查看是否有购物车数据');
        utils.getCart(this,true);
    },
    // retain:{
    //
    // },
    //购物车和商品列表数据做配对
    dataPairing:function(){
        let that=this;
        console.log('购物车和商品列表数据做配对');
        var  cartData=this.data.cartDataList;
        let pList=this.data.details;
        console.log(cartData);
        console.log(pList);
        //清空显示数据
        if(cartData[0]){
            console.log('清空显示数据');
            pList.forEach(function(pValue, index, array) {
                pValue.cartNum=undefined;
            });
            console.log('清空后的数据');
            console.log(pList);
            //console.log('循环配对');
            pList.forEach(function(pValue, index, array) {
                let pid=pValue.id;
                if(cartData[0].buydata){
                    cartData[0].buydata.forEach(function(cValue, index, array) {
                        var cid=cValue.goodsid;
                        //////console.log(cid);
                        if(cid==pid){
                            console.log('找到配对');
                            pValue.cartNum=cValue.buyNum;
                            console.log(pValue);
                        }
                        //else if(cValue.buyNum!=0&&)
                    })

                }
            });
            that.setData({
                details:pList
            })
        }

    },
    //选好了去购物车
    // toCheck:function(){
    //     ////console.log('选好了去购物车');
    //     wx.navigateTo({
    //         url: 'page/cart/cart'
    //     })
    // },
    //增加新功能///////////////////////////////////////////
    //选中商品属性
    selectedAttribute:function(e){
      console.log('选中商品属性');
        ////console.log(e.currentTarget.id);
        let attribute=this.data.attributeData;
        let indexs=parseInt(e.currentTarget.id);
        attribute.forEach(function(value,index,arr){
            value.ismr=0;
        });
        attribute[indexs].ismr=1;
        console.log(attribute);
        console.log(indexs);
        this.setData({
            attributeData:attribute
        })

    },
    //隐藏规格功能
    specifHidden:function(){
        ////console.log('隐藏规格功能');
        this.setData({
            specifShow:false
        });
    },
    //显示规格功能
    specifShow:function(e){
        console.log('显示规格功能');
        ////console.log(e.currentTarget.id);
        let pDetail=this.data.details[e.currentTarget.id];
        let selectData=[];
        console.log('商品信息');
        console.log(pDetail);
        //兼容不完整信息
        pDetail.property.forEach(function(value,index,arr){
           if(!value.choose_count){
               value.choose_count=1;
           }
            // value.property.forEach(function(val,ind,Arr){
            //
            // });
           //设置默认选中值
            console.log(value);
           value.property[0].isSel=true;
           selectData[index]=[];

           let obj=[
               {
                   i:'0',
                   val:value.property[0].val
               }
           ];
           console.log(selectData[index]);
           selectData[index].indexs=obj;


        });
        this.setData({
            specifData:pDetail,
            specifShow:true,
            selectData
        });
        console.log('设置好的数据');
        console.log(this.data.specifData);
        console.log(selectData);
        this.specifications();
    },
    //组件的方法
    specifications:function(){
        ////console.log('组件内的方法');
        ////console.log();
    },
    //增加新功能///////////////////////////////////////////
    //兼容最大选项为空的商品列表

});


